<?php
include_once("header.php");
include_once("common/dbConnect.php");
$handle = db_connect();
if(!$handle){
    echo "Did not access to the database";
}
$ID=(int)$_GET["id"];
if($ID>0){
    $student_sql = "select * from Student where StudentID=".$ID;    //php中用.串联字符串
    $result = mysql_query($student_sql);
    $entity = mysql_fetch_array($result);
}
?>

<script type="text/javascript">
    $(document).ready(function() {
        $("#btn_edit").click(function() {
            var name = $("#student_name").val();
            var age =  $("#student_age").val();
            var tel =  $("#student_tel").val();
            var id =  $("#hd_stuId").val();
            if($.trim(name)==''){
                alert("请输入姓名！");
            }
            else if($.trim(age)==''){
                alert("请输入年龄！");
            }
            else if($.trim(tel)==''){
                alert("请输入电话号码！");
            }
            else {
                var form_data = {};
                form_data.name = name;
                form_data.age = age;
                form_data.tel = tel;
                form_data.id= id;
                $.ajax({
                    type: "POST",
                    url: 'edit_submit.php',
                    data: form_data,
                    success: function (response) {
                        if (response == 'success')
                            window.location.href = 'index.php';
                    }
                });
            }
            return false;
        });
    });
</script>
<div class="container body-content">
    <h2>Edit</h2>
    <div class="form-horizontal">
        <h4></h4>
        <hr />
        <form id="form1" name="form1" method="post">
            <input type="hidden" id="hd_stuId" value="<?php echo $entity['StudentID'] ?>"></text>
            <div class="form-group">
                <label class="control-label col-md-2">Name</label>
                <div class="col-md-10">
                    <input class="form-control text-box single-line" id="student_name" name="student_name" type="text" value="<?php echo $entity['StudentName'] ?>" />
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-2">Age</label>
                <div class="col-md-10">
                    <input class="form-control text-box single-line" id="student_age" name="student_age" type="text"  value="<?php echo $entity['StudentAge'] ?>"/>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-2">Tel</label>
                <div class="col-md-10">
                    <input class="form-control text-box single-line" id="student_tel" name="student_tel" type="text"  value="<?php echo $entity['StudentTel'] ?>"/>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" id="btn_edit" name="btn_edit" value="Save" class="btn btn-default" />&nbsp;&nbsp;&nbsp;
                    <a href="index.php" class="btn btn-default">Return</a>
                </div>
            </div>
        </form>
    </div>
</div>
